package com.ybkj.o2o.zhuanxian.service.impl;

import com.ybkj.o2o.zhuanxian.entity.Car;
import com.ybkj.o2o.zhuanxian.mapper.CarMapper;
import com.ybkj.o2o.zhuanxian.service.CarService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DefaultCarService implements CarService {

  @Autowired
  private CarMapper carMapper;

  @Override
  public Car create(Car car) {
    carMapper.insert(car);
    return car;
  }

  @Override
  public Car findByEmploy(Long employId, String appKey) {

    return carMapper.findByEmploy(employId, appKey);
  }

  @Override
  public int update(Car car) {

    return carMapper.update(car);
  }
  @Override
  public int updateEmployIdToNull(Car car) {

    return carMapper.updateEmployIdToNull(car);
  }
  @Override
  public Car findOne(Long carId, String appKey) {
    return carMapper.finByIdAndAppKey(carId, appKey);
  }

  @Override
  public int deleteByIdAndAppKey(Long carId, String appKey) {

    return carMapper.deleteByIdAndAppKey(carId, appKey);
  }

  @Override
  public List<Car> findByParam(Long lineId, String lineName, Long companyId, String companyName,
      String carName, String carNumber, Integer[] status, Long employId, String employName,
      Integer limit, Integer rows, String appKey) {

    return carMapper.findByParam(lineId, StringUtils.isNotBlank(lineName) ? "%" + lineName + "%"
        : null, companyId, StringUtils.isNotBlank(companyName) ? "%" + companyName + "%" : null,
        StringUtils.isNotBlank(carName) ? "%" + carName + "%" : null, StringUtils
            .isNotBlank(carNumber) ? "%" + carNumber + "%" : null, status != null
            && status.length > 0 ? status : null, employId,
        StringUtils.isNotBlank(employName) ? "%" + employName + "%" : null, limit, rows, appKey);
  }

  @Override
  public List<Car> findCarListByParams(String lineName,Long companyId, String companyName, String carNumber, Integer[] status, Long employId, String employName, Integer limit, Integer rows, String appKey) {
    return carMapper.findCarListByParams(StringUtils.isNotBlank(lineName)?"%"+lineName+"%":null, companyId, StringUtils.isNotBlank(companyName) ? "%" + companyName + "%" : null,StringUtils
                    .isNotBlank(carNumber) ? "%" + carNumber + "%" : null, status != null
                    && status.length > 0 ? status : null, employId,
            StringUtils.isNotBlank(employName) ? "%" + employName + "%" : null, limit, rows, appKey);
  }

  @Override
  public Long countParam(Long lineId, String lineName, Long companyId, String companyName,
      String carName, String carNumber, Integer[] status, Long employId, String employName,
      String appKey) {
    return carMapper.countParam(lineId, StringUtils.isNotBlank(lineName) ? "%" + lineName + "%"
        : null, companyId, StringUtils.isNotBlank(companyName) ? "%" + companyName + "%" : null,
        StringUtils.isNotBlank(carName) ? "%" + carName + "%" : null, StringUtils
            .isNotBlank(carNumber) ? "%" + carNumber + "%" : null, status != null
            && status.length > 0 ? status : null, employId,
        StringUtils.isNotBlank(employName) ? "%" + employName + "%" : null, appKey);
  }

  @Override
  public Long countParams(Long lineId, String lineName, Long companyId, String companyName,
                         String carName, String carNumber, Integer[] status, Long employId, String employName,
                         String appKey) {
    return carMapper.countParams(lineId, StringUtils.isNotBlank(lineName) ? "%" + lineName + "%"
                    : null, companyId, StringUtils.isNotBlank(companyName) ? "%" + companyName + "%" : null,
            StringUtils.isNotBlank(carName) ? "%" + carName + "%" : null, StringUtils
                    .isNotBlank(carNumber) ? "%" + carNumber + "%" : null, status != null
                    && status.length > 0 ? status : null, employId,
            StringUtils.isNotBlank(employName) ? "%" + employName + "%" : null, appKey);
  }

  @Override
  public int updateCarLineToNull(Long lineId, String appKey) {
    return carMapper.updateCarLineToNull(lineId, appKey);
  }

  @Override
  public List<Car> findBycarNumber(String carNumber, String appKey) {
    return carMapper.findBycarNumber(StringUtils.isNotBlank(carNumber) ? "%" + carNumber + "%"
        : null, appKey);
  }

  @Override
  public List<Car> findByIds(List<Long> ids) {

    return carMapper.findByIds(ids);
  }

  @Override
  public Car findByNumber(String carNumber, String appKey) {
    return carMapper.selectByNumber(carNumber,appKey);
  }

  @Override
  public Car queryCarByNumber(String carNumber, String appKey) {
    return carMapper.selectByCarNumber(carNumber, appKey);
  }

@Override
public int updateEmployIdToNullById(Car car) {
	return carMapper.updateEmployIdToNullById(car);
}

}
